package com.itheima.d2_ThreadPool;

import java.util.concurrent.*;

/**
 * 线程池接口：Executor
 *                  实现类：ThreadPoolExecutor
 *                  构造器参数： 1.int corePoolSize
 *                             2.int maximumPoolSize
 *                             3.long keepAliveTime    临时线程的最大存活时间
 *                             4.TimeUnit unit
 *                             5.BlockingQueue<Runnable> workQueue
 *                             6.ThreadFactory threadfactory
 *                             7.RejectedExecutionHandler handler 新任务的处理办法
 *
 */
public class Runnable_ThreadPool {
    public static void main(String[] args) {
        //1.创建方法一
        ExecutorService executorService = new ThreadPoolExecutor(3,
                5,
                5,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5)
                ,Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );

        //2.传入任务
        Runnable r = new MyRunnable();
        //核心线程数量为3，先来的三个必然可以执行
        executorService.execute(r);
        executorService.execute(r);
        executorService.execute(r);
        //等待队列为5，当等待队列没有满时，不会去创建临时线程
        executorService.execute(r);
        executorService.execute(r);
        executorService.execute(r);
        executorService.execute(r);
        executorService.execute(r);
        //开始创建临时线程，maximum代表线程的最多个数，即  maximum = core + alive
        executorService.execute(r);
        executorService.execute(r);

        //会报错
        //executorService.execute(r);



    }
}


class MyRunnable implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            //System.out.println(Thread.currentThread().getName()+"输出了"+ i);
            try {
                System.out.println(Thread.currentThread().getName()+"绑定休眠");
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}